import { DraftContent } from '@models/DraftContent';
import { VideoTrack, AudioTrack, TextTrack, StickerTrack } from '../models/Track';
import { createVideoSegment, createAudioSegment } from '../models/Segment';
import { saveDraft, loadDraft } from '../utils/file';
import { secondsToMicroseconds } from '../utils/time';
import { AudioMaterial, StickerMaterial, TextMaterial, VideoMaterial } from '@models/Material';
import { Service } from 'typedi';
import { DraftModel } from '@models/DraftModel';
import * as console from 'console';

@Service()
export class JydraftscomService {
  private content: DraftContent;

  constructor() {
    this.content = {
      app_version: '5.9.0',
      new_version: '5.9.0',
      materials: {
        videos: [],
        audios: [],
        texts: [],
        stickers: [],
      },
      tracks: [],
      version: 1,
    };
  }

  getDraft(): DraftContent {
    const dft = DraftModel;
    // {
    //   app_version: '5.9.0',
    //   new_version: '5.9.0',
    //   materials: {
    //     videos: [],
    //     audios: [],
    //     texts: [],
    //     stickers: [],
    //   },
    //   tracks: [],
    //   version: 1,
    // };
    return <DraftContent>dft;
  }
  // 添加视频素材
  addVideo(path: string, duration: number, name = 'Video'): VideoMaterial {
    const id = `v${Date.now()}`;
    const videoMaterial: { duration: number; path: string; name: string; id: string } = {
      id,
      path,
      duration: secondsToMicroseconds(duration),
      name,
    };

    this.content.materials.videos.push(<VideoMaterial>videoMaterial);
    return <VideoMaterial>videoMaterial;
  }

  // 添加音频素材
  addAudio(path: string, duration: number, name = 'Audio'): AudioMaterial {
    const id = `a${Date.now()}`;
    const audioMaterial: { duration: number; path: string; name: string; id: string } = {
      id,
      path,
      duration: secondsToMicroseconds(duration),
      name,
    };

    this.content.materials.audios.push(<AudioMaterial>audioMaterial);
    return <AudioMaterial>audioMaterial;
  }

  // 添加文本素材
  addText(dft: DraftContent, content: string, fontSize = 48, color = '#FFFFFF'): TextMaterial {
    const id = `t${Date.now()}`;
    console.log('---->id ', id);
    const textMaterial: {
      duration: number;
      path: string;
      color: string;
      name: string;
      fontSize: number;
      id: string;
      content: string;
    } = {
      id,
      path: '',
      duration: 0,
      name: 'Text',
      content,
      fontSize,
      color,
    };

    // this.content.materials.texts.push(<TextMaterial>textMaterial);
    dft.materials.texts.push(<TextMaterial>textMaterial);
    return <TextMaterial>textMaterial;
  }

  // 添加贴纸素材
  addSticker(path: string, duration: number, animation = 'bounce'): StickerMaterial {
    const id = `s${Date.now()}`;
    const stickerMaterial: { duration: number; path: string; name: string; id: string; animation: string } = {
      id,
      path,
      duration: secondsToMicroseconds(duration),
      name: 'Sticker',
      animation,
    };

    this.content.materials.stickers.push(<StickerMaterial>stickerMaterial);
    return <StickerMaterial>stickerMaterial;
  }

  // 添加视频轨道
  addVideoTrack(): VideoTrack {
    const track = new VideoTrack();
    this.content.tracks.push(track);
    return track;
  }

  // 添加音频轨道
  addAudioTrack(): AudioTrack {
    const track = new AudioTrack();
    this.content.tracks.push(track);
    return track;
  }

  // 添加文本轨道
  addTextTrack(): TextTrack {
    const track = new TextTrack();
    this.content.tracks.push(track);
    return track;
  }

  // 添加贴纸轨道
  addStickerTrack(): StickerTrack {
    const track = new StickerTrack();
    this.content.tracks.push(track);
    return track;
  }

  // 在轨道添加视频片段
  addVideoSegment(track: VideoTrack, materialId: string, start: number, duration: number) {
    track.segments.push(createVideoSegment(materialId, secondsToMicroseconds(start), secondsToMicroseconds(duration)));
  }

  // 在轨道添加音频片段
  addAudioSegment(track: AudioTrack, materialId: string, start: number, duration: number) {
    track.segments.push(createAudioSegment(materialId, secondsToMicroseconds(start), secondsToMicroseconds(duration)));
  }

  // 保存草稿文件
  save(path: string): void {
    saveDraft(this.content, path);
  }

  // 保存草稿文件
  saveDft(dft: DraftContent, path: string): void {
    saveDraft(dft, path);
  }

  getTstDraftMsg(): string {
    return 'Hello ketty!';
  }

  // 加载现有草稿文件
  // static load(path: string): JydraftscomService {
  //   const content = loadDraft(path);
  //   const draft = new JydraftscomService();
  //   draft.content = content;
  //   return draft;
  // }
}
